package com.webapp.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ParamUtil {

	String[] radioParamName= new String[] {"P_PROJECT","P_YEAR","P_SalaryGrade"};
	String[] checkboxParamName= new String[] {"P_OFFICE","P_COST_CTR","P_ENTITY"};
	
	public List<String> getParamListString(JSONObject jsonObject){
		List<Map<String, JSONArray>> list = new ArrayList<Map<String,JSONArray>>();
		for (String string : checkboxParamName) {
			JSONArray array = jsonObject.getJSONArray(string);
			if(array != null && array.size() > 1) {
				Map<String, JSONArray> map = new HashMap<String,JSONArray>();
				map.put(string, array);
				list.add(map);
			}
		}
		List<String> resultList = getParam(list);
		return resultList;
	}
	
	/**
	 * 单选条件格式
	 * @param jsonObject
	 * @return
	 */
	public String getParamString(JSONObject jsonObject){
		String paramString = "";
		for (String string : radioParamName) {
			if(!StringUtils.isEmpty(jsonObject.getString(string))) {
				paramString += string+"="+jsonObject.getString(string)+"|";
			}
		}
		if(!StringUtils.isEmpty(paramString)) {
			paramString = paramString.substring(0, paramString.length()-1);
		}
		return paramString;
	}
	
	
	
	
	public List<String> getParam(List<Map<String, JSONArray>> list){
		if (list.size()==1) {
			return getParam(list.get(0));
		}else if(list.size() == 2) {
			return getParam(list.get(0),list.get(1));
			
		}else if(list.size() == 3) {
			return getParam(list.get(0),list.get(1),list.get(2));
		}
		return null;
	}
	public List<String> getParam(Map<String, JSONArray> map){
		List<String> list = new ArrayList<String>();
		Map<String, String> result = new HashMap<String,String>();
		for (Entry<String, JSONArray> entry : map.entrySet()) {
			JSONArray array = entry.getValue();
			for (Object object : array) {
				result.put(entry.getKey(), object.toString());
				list.add(mapToString(result));
			}
		}
		return list;
	}
	public List<String> getParam(Map<String, JSONArray> map, Map<String, JSONArray> map2){
		List<String> list = new ArrayList<String>();
		Map<String, String> result = new HashMap<String,String>();
		for (Entry<String, JSONArray> entry : map.entrySet()) {
			JSONArray array = entry.getValue();
			for (Object object : array) {
				
				result.put(entry.getKey(), object.toString());
				for (Entry<String, JSONArray> entry2 : map2.entrySet()) {
					JSONArray array2 = entry2.getValue();
					for (Object object2 : array2) {
						
						result.put(entry2.getKey(), object2.toString());
						list.add(mapToString(result));
					}
				}
			}
		}
		return list;
	}
	public List<String> getParam(Map<String, JSONArray> map, Map<String, JSONArray> map2,Map<String, JSONArray> map3){
		List<String> list = new ArrayList<String>();
		Map<String, String> result = new HashMap<String,String>();
		for (Entry<String, JSONArray> entry : map.entrySet()) {
			JSONArray array = entry.getValue();
			for (Object object : array) {

				result.put(entry.getKey(), object.toString());
				for (Entry<String, JSONArray> entry2 : map2.entrySet()) {
					JSONArray array2 = entry2.getValue();
					for (Object object2 : array2) {

						result.put(entry2.getKey(), object2.toString());
						for (Entry<String, JSONArray> entry3 : map3.entrySet()) {
							JSONArray array3 = entry3.getValue();
							for (Object object3 : array3) {

								result.put(entry3.getKey(), object3.toString());
								list.add(mapToString(result));
							}
						}
					}
				}
			}
		}
		return list;
	}
	
	
	public String mapToString (Map<String, String> map) {
		String string = "";
		for (Entry<String, String> entry : map.entrySet()) {
			string += entry.getKey()+"="+entry.getValue()+"|";
		}
		if (!StringUtils.isEmpty(string)) {
			string = string.substring(0,string.length()-1);
		}
		return string;
	}
	
	
	
	
	public static String getSheelName (String string) {
		string = string.replaceAll(" ", "_");
		string = string.replaceAll("/", "_");
		string = string.replaceAll("\\|", "&");
//		String result = "";
//		if(string.indexOf("|") > 0) {
//			String[] strs = string.split("\\|");
//			for (String string2 : strs) {
//				result += string2.split("=")[1] + "|";
//			}
//		}else {
//			result = string.split("=")[1];
//		}
//		if(!StringUtils.isEmpty(result) && result.endsWith("|")) {
//			result = result.substring(0,result.length()-1);
//		}
		return string;
	}
	
	
	
	
	
	
	
	
	
}
